const Router = require("koa-router");
const { Reply, Complaint } = require("@models/complaint");
const { BlackList } = require("@models/blacklist");
const { Auth } = require("@middlewares/auth");
const { User } = require("@models/user");
const { Admin } = require("@models/admin");


const router = new Router({
  prefix: "/admin/complaint",
});
// Reply.sync({ force: true });
// 留言列表
router.post("/list", new Auth(16).m, async (ctx, next) => {
  const { page = 1, pageSize = 10 } = ctx.request.body;
  const offset = (page - 1) * pageSize;

  const { count: total, rows } = await Complaint.findAndCountAll({
    limit: pageSize, // 每页数量
    offset, // 偏移量
    order: [["created_at", "DESC"]],
    include: [
      {
        model: User,
        attributes: ["uid", "user_name", "avatar"],
        required: false,
        include: [
          {
            model: BlackList,
            required: false,
          },
        ],
      },

      {
        model: Reply,
        attributes: ["reply_id", "admin_id", "content", "created_at"],
        required: false,
        include: [
          {
            model: Admin,
            attributes: ["admin_name", "avatar"],
            required: false,
          },
        ],
      },
    ],
  });

  ctx.body = {
    code: 200,
    msg: "获取成功",
    total,
    data: rows,
  };
});

// 禁言
router.post("/blacklist", new Auth(16).m, async (ctx) => {
  const { uid } = ctx.request.body;
  const blacklist = await BlackList.findOne({ where: { uid } });
  if (!blacklist) {
    const complaint = await BlackList.create({ uid });
    if (!complaint) {
      throw new global.errs.AuthFailed("添加失败");
    }
    ctx.body = {
      code: 200,
      msg: "添加成功",
    };
  } else {
    throw new global.errs.AuthFailed("该用户已被禁言");
  }
});


// 取消禁言
router.post("/unblacklist", new Auth(16).m, async (ctx) => {
  const { uid } = ctx.request.body;
  await BlackList.destroy({ where: { uid }, force: true });
  ctx.body = {
    code: 200,
    msg: "删除成功",
  };
});

// 删除留言
router.post("/delete", new Auth(16).m, async (ctx) => {
  const { complaint_id } = ctx.request.body;
  await Complaint.destroy({ where: { complaint_id }, force: true });
  ctx.body = {
    code: 200,
    msg: "删除成功",
  };
});

// 删除回复
router.post("/reply/delete", new Auth(16).m, async (ctx) => {
  const { reply_id } = ctx.request.body;
  await Reply.destroy({ where: { reply_id }, force: true });
  ctx.body = {
    code: 200,
    msg: "删除成功",
  };
});

// 修改回复
router.post("/reply/update", new Auth(16).m, async (ctx) => {
  const { reply_id, content } = ctx.request.body;

   const reply = await Reply.findOne({ where: { reply_id } });
   reply.update({ content });

    ctx.body = {
      code: 200,
      msg: "修改成功",
    };

});

// 回复留言
router.post("/reply", new Auth(16).m, async (ctx) => {
  const { complaint_id, content } = ctx.request.body;
  try {
    await Reply.create({ complaint_id, content, admin_id: ctx.auth.uid });
    ctx.body = {
      code: 200,
      msg: "回复成功",
    };
  } catch (error) {
    throw new global.errs.AuthFailed(error.message);
  }
});

// 是否显示留言
router.post("/on", new Auth(16).m, async (ctx) => {
  const { is_on, complaint_id } = ctx.request.body;
  try {
    await Complaint.update({ is_on }, { where: { complaint_id } });
    ctx.body = {
      code: 200,
      msg: "操作成功",
    };
  } catch (error) {
    throw new global.errs.AuthFailed(error.message);
  }

});

module.exports = router;
